package myJs.api

import japgolly.scalajs.react.extra.Ajax
import myJs.myPkg.MyFetch
import upickle.default._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._
import shared.pojo.Pojo.ValidData

/** Created by yz on 18/10/2022
  */
object WorkflowMission {

  import shared.pojo.Pojo.workflowMission._

  val baseUrl = s"${Api.apiUrl}/user/workflow"

  def missionNameCheck(request: MissionNameCheck.Request) = {
    val url = s"${baseUrl}/missionNameCheck"
    MyFetch
      .postRnText(url = s"${url}", data = write(request), headers = Map("Content-Type" -> "application/json"))
      .map { str =>
        read[ValidData](str)
      }
  }

  def featureSelection(request: FsData) = {
    val url = s"${baseUrl}/fs"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[ValidData](xhr.responseText)
      }
  }

  def configData(missionId: String) = {
    val url = s"${baseUrl}/configData?missionId=${missionId}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[FsData](xhr.responseText)
      }
  }

  def getConfigData(fileName: String) = {
    val url = s"${baseUrl}/configData?fileName=${fileName}"
    Ajax
      .get(s"${url}")
      .setRequestContentTypeJson
      .send
      .asAsyncCallback
      .map { xhr =>
        read[FsData](xhr.responseText)
      }
  }

  def fileExist(request: FileExistData) = {
    val url = s"${baseUrl}/fileExist"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[Boolean](xhr.responseText)
      }
  }

  def inputMetabolites(request: InputMetabolitesRq) = {
    val url = s"${baseUrl}/inputMetabolites"
    Ajax
      .post(s"${url}")
      .setRequestContentTypeJson
      .send(write(request))
      .asAsyncCallback
      .map { xhr =>
        read[List[String]](xhr.responseText)
      }
  }

}
